<template>
  <div style="position: relative;">
    <el-form :inline="true" size="mini" label-position="right">
      <el-form-item>
        <el-button type="primary" @click="loadAttendanceWidgetData()">搜索</el-button>
        <el-button type="primary" @click="addFromdata()">新增考勤组</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray" v-loading="isShowloading">
          <el-table-column type="index" align="center" width="100" label="序号"></el-table-column>
          <el-table-column prop="groupName" align="center" label="考勤组" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="personCount" align="center" label="考勤人数" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="groupWeek" align="center" label="工作日" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="clockInTime" align="center" label="上班时间" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="createUserName" align="center" label="创建人" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
              <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"></el-pagination>
        </el-row>
      </el-col>
    </el-row>
    <addAttendance v-if="addfrombox" ref="addAttendance" v-model="addfrombox"
    :createUserId="createUserId"></addAttendance>
  </div>
</template>

<script>
import { AttendanceSettings } from '@/api/attenDances.js';
import addAttendance from './addAttendance.vue';
export default {
  name: 'AttendanceSettings',
  components: { addAttendance },
  data () {
    return {
      centerDialogVisible: false,
      editDialogVisible: false,
      addfrombox: false,
      showDetail: {},
      showEdit: {},
      tableData: [],
      villageData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false,
      Addroptions: [],
      defaultProps: {
        children: 'nodes',
        label: 'name'
      },
      createUserId: ''
    }
  },
  created () {
  },
  mounted () {
    this.loadAttendanceWidgetData()
  },
  activated () {
    this.loadAttendanceWidgetData()// 数据加载的方法
  },
  computed: {},
  methods: {
    /**
     * 考勤设置列表
     */
    loadAttendanceWidgetData () {
      let params = {
        size: this.page_size,
        current: this.pageNo
      }
      return new Promise((resolve, reject) => {
        AttendanceSettings.list(this, params).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
        }).catch(e => {
          reject(e);
        });
      });
    },
    onSubmit () {
      this.pageNo = 1
      this.loadAttendanceWidgetData()
    },
    // 重置
    resetFromdata () {
      this.loadAttendanceWidgetData()
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadAttendanceWidgetData()
    },
    handleSizeChange (val) {
      this.page_size = val
      this.loadAttendanceWidgetData()
    },
    currentChange (val) {
      this.pageNo = val
      this.loadAttendanceWidgetData()
    },
    // 新增
    addFromdata () {
      this.$router.push('/addAttendance');
    },
    // 修改
    handleEdit (data) {
      this.$router.push({path: '/addAttendance', query: {groupId: data.groupId}})
    },
    // 删除
    handleDelete (data) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = data.groupId
        return new Promise((resolve, reject) => {
          AttendanceSettings.delete(this, params).then(res => {
            this.$message.success('删除成功！');
            this.loadAttendanceWidgetData();
          }).catch(e => {
            reject(e);
          });
        });
      })
    }
  }
}
</script>
